package com.wohuijituan.whcsmerchant.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.fragment.GoodsSellFragment;

/**
 * Created by zsy_18 on 2017/1/3.
 */

public class ShopGoodsSearchActivity extends MyBaseActivity implements View.OnClickListener{
    private FrameLayout flContentSearch;
    private ImageButton titleGoBack;
    private TextView titleName;
    private EditText titleEdit;
    private TextView titleTvAction;
    private ImageButton titleIbAction;
    private GoodsSellFragment fragment;

    private void assignViews() {
        titleGoBack = (ImageButton) findViewById(R.id.title_go_back);
        titleName = (TextView) findViewById(R.id.title_name);
        titleEdit = (EditText) findViewById(R.id.titleEdit);
        titleTvAction = (TextView) findViewById(R.id.title_tv_action);
        titleIbAction = (ImageButton) findViewById(R.id.title_ib_action);
        flContentSearch = (FrameLayout) findViewById(R.id.fl_content_search);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_search);
        assignViews();
        initData();
        initView();
        initListener();
    }
    private void initData() {

    }
    private void initView() {
        titleName.setVisibility(View.GONE);
        titleEdit.setVisibility(View.VISIBLE);
        titleGoBack.setVisibility(View.VISIBLE);
        titleIbAction.setVisibility(View.VISIBLE);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        fragment = (GoodsSellFragment) fm.findFragmentByTag(GoodsSellFragment.TAG);
        if (fragment ==null){
            fragment = GoodsSellFragment.newInstance(GoodsSellFragment.STYLE_SEARCH);
        }
        ft.replace(R.id.fl_content_search, fragment, GoodsSellFragment.TAG);
        ft.commit();
    }
    private void initListener() {
        titleGoBack.setOnClickListener(this);
        titleIbAction.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_go_back:
                onBackPressed();
                break;
            case R.id.title_ib_action:
                if (fragment!=null){
                    String keyWord = titleEdit.getText().toString().trim();
                    if ("".equals(keyWord)){
                        showToast(ctx,"搜索不能为空!");
                        return;
                    }
                    fragment.doSearchGoods(keyWord);
                }else {
                    showToast(ctx,"未知错误,请重新打开本页面!");
                }
        }
    }
}
